package com.adb.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.adb.entity.DataRect;
import com.jjmmbb.jxSudoku.GameActivity;
import com.jjmmbb.jxSudoku.R;

public class DraftDialog extends Dialog implements OnClickListener {
	Button b1, b2, b3, b4, b5, b6, b7, b8, b9, bOK, bClear;
	DataRect dr;// 目标方格
	char[] arrDraft = new char[9];

	public DraftDialog(Context context, DataRect dr) {
		super(context);
		this.dr = dr;
	}

	public DraftDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);
		super.onCreate(savedInstanceState);

		setContentView(R.layout.dialog);

		b1 = (Button) findViewById(R.id.dialog_b1);
		b2 = (Button) findViewById(R.id.dialog_b2);
		b3 = (Button) findViewById(R.id.dialog_b3);
		b4 = (Button) findViewById(R.id.dialog_b4);
		b5 = (Button) findViewById(R.id.dialog_b5);
		b6 = (Button) findViewById(R.id.dialog_b6);
		b7 = (Button) findViewById(R.id.dialog_b7);
		b8 = (Button) findViewById(R.id.dialog_b8);
		b9 = (Button) findViewById(R.id.dialog_b9);
		bClear = (Button) findViewById(R.id.dialog_clear);
		bOK = (Button) findViewById(R.id.dialog_ok);

		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
		b3.setOnClickListener(this);
		b4.setOnClickListener(this);
		b5.setOnClickListener(this);
		b6.setOnClickListener(this);
		b7.setOnClickListener(this);
		b8.setOnClickListener(this);
		b9.setOnClickListener(this);
		bClear.setOnClickListener(this);
		bOK.setOnClickListener(this);

		// LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		// p.width = (int) (Comm.screenWidth * 0.8); // 设置对话框宽度80%
		ini();
	}

	void ini() {
		String draftCode = dr.getDraftCode();
		int length = 9;
		if (draftCode.length() < 9) {
			length = draftCode.length();
		}
		for (int i = 0; i < length; i++) {
			arrDraft[i] = draftCode.charAt(i);
		}
		iniBt();
	}

	void iniBt() {
		setButtonBackground(b1, arrDraft[0]);
		setButtonBackground(b2, arrDraft[1]);
		setButtonBackground(b3, arrDraft[2]);
		setButtonBackground(b4, arrDraft[3]);
		setButtonBackground(b5, arrDraft[4]);
		setButtonBackground(b6, arrDraft[5]);
		setButtonBackground(b7, arrDraft[6]);
		setButtonBackground(b8, arrDraft[7]);
		setButtonBackground(b9, arrDraft[8]);
	}

	void setButtonBackground(Button bt, char c) {
		if (c == '1') {
			bt.setBackgroundResource(R.drawable.btn_bg_5);
		} else {
			bt.setBackgroundResource(R.drawable.btn_bg_2);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_b1:
			setDraftStr(1);
			break;
		case R.id.dialog_b2:
			setDraftStr(2);
			break;
		case R.id.dialog_b3:
			setDraftStr(3);
			break;
		case R.id.dialog_b4:
			setDraftStr(4);
			break;
		case R.id.dialog_b5:
			setDraftStr(5);
			break;
		case R.id.dialog_b6:
			setDraftStr(6);
			break;
		case R.id.dialog_b7:
			setDraftStr(7);
			break;
		case R.id.dialog_b8:
			setDraftStr(8);
			break;
		case R.id.dialog_b9:
			setDraftStr(9);
			break;
		case R.id.dialog_clear:
			clearDraftStr();
			dr.setDraft(arrToString(), this.getContext());
			GameActivity.gameView.invalidate();
			this.dismiss();
			break;
		case R.id.dialog_ok:
			dr.setDraft(arrToString(), this.getContext());
			GameActivity.gameView.invalidate();
			this.dismiss();
			break;
		}
	}

	void clearDraftStr() {
		for (int i = 0; i < arrDraft.length; i++) {
			arrDraft[i] = 0;
		}
	}

	String arrToString() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arrDraft.length; i++) {
			sb.append(arrDraft[i]);
		}
		System.out.println(sb.toString());
		return sb.toString();
	}

	// 添加或取消一个草稿
	void setDraftStr(int number) {
		number--; // 从0开始
		if (arrDraft[number] == '1') {
			arrDraft[number] = '0';
		} else {
			// 检查,草稿数量,草稿>2个 时不能再添加草稿
			int count = 0;
			for (int i = 0; i < arrDraft.length; i++) {
				if (arrDraft[i] == '1') {
					count++;
				}
				if (count >= 3) { // 每个方格最多添加2个草稿
					return;
				}
			}

			arrDraft[number] = '1';
		}
		iniBt();
		System.out.println(arrDraft);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.dismiss();
		}
		return true;
	}
}
